package nadrai;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import org.jfugue.Note;
import org.w3c.dom.ranges.RangeException;

public class Motif {
	
	private ArrayList<Note> motifNotes = new ArrayList<Note>();
	
	private String[] durations = {"w","h","q","i","s","t","x","o"};
	private String[] scale = {"C","D","E","F","G","A","H"};
	private ArrayList<Byte> possibleNotes = new ArrayList<Byte>();
	private String pattern;

	public Motif(int motifLength) {
		generatePossibleNotes();
		setPattern(createBaseNotes(motifLength));
	}


	private void generatePossibleNotes() {
		for (byte b = 0; b < 127; b++) {

			Note note = new Note();
			note.setValue(b);
			// System.out.println("b:"+b);
			// System.out.println("note musicstring:"+note.getMusicString());

			for (String scaleElement : scale) {
				if (note.getMusicString().contains(scaleElement)
						&& !note.getMusicString().contains(scaleElement + "#")
						&& !note.getMusicString().contains(scaleElement + "b")) {
					possibleNotes.add(note.getValue());
					System.out.println("possible good note found:"
							+ note.getMusicString());
				}

			}

		}
			
	}



	public void createMotif() {
		
		
	}
	
	public int getRandomJumpValue(int min, int max){
		Random randomGenerator = new Random();
		return randomGenerator.nextInt(max-min)+min;
	}
	
	public String createBaseNotes(int motifLength) {
		ArrayList<Integer> rythm = createRythm(motifLength);
		System.out.print("rythm:");
		for (Iterator<Integer> iterator = rythm.iterator(); iterator.hasNext();) {
			Integer rythmvalue = iterator.next();
			System.out.print(durations[rythmvalue]+" ");
			
		}
		System.out.println();
		Note note = new Note();
		
		//pick first note
		int index = Math.round(possibleNotes.size()/2);
		
		Byte actualNote = possibleNotes.get(index);
		note.setValue(actualNote);
		
		System.out.println("1. note:"+note.getMusicString());
		motifNotes.add(note);
		
		for (int i = 0; i < rythm.size()-1; i++) {
			int jump = getRandomJumpValue(-3, 3);
			index=index+jump;
			Note tempNote = new Note();
			tempNote.setValue(possibleNotes.get(index));
			
			System.out.print(" "+tempNote.getMusicString());
			motifNotes.add(tempNote);
			//System.out.println("jump:"+jump+" | "+(i+2)+". note:"+tempNote.getMusicString());
		}
		
		
		String song=getMusicString(motifNotes,rythm);
		
		
		 //System.out.println("song in createbasenotes:"+song);
		return song;
		
	}

	private String getMusicString(ArrayList<Note> motifNotes, ArrayList<Integer> rythm) {
		String song ="T240 ";
		int index =0;
		
		System.out.println("motifnotes.size"+motifNotes.size());
		System.out.println("rythm:"+rythm.size());
		
		
		for (Iterator<Note> iterator = motifNotes.iterator(); iterator.hasNext();) {
			Note note = iterator.next();
			//System.out.println("actualnote"+note.getMusicString());
			song+=note.getMusicString().replace("/0.0", "")+durations[rythm.get(index)]+" ";
			System.out.println(song);
			index++;
		}
		return song;
		
		
		
	}

	private ArrayList<Integer> createRythmOneBar() {
		ArrayList<Integer> rythm = new ArrayList<Integer>();
		
			rythm.add(0);
		
		
		Random randomGenerator = new Random();
		int doSomething = randomGenerator.nextInt(119);
		
		while (doSomething!=0) {
			doSomething = randomGenerator.nextInt(3);
			int randomIndex = randomGenerator.nextInt(rythm.size());
			int value = rythm.get(randomIndex);
			if (value!=7) {
				rythm.set(randomIndex, value+1);
				rythm.add(randomIndex+1, value+1);
			}
		}
		
		return rythm;
	}

	
	private ArrayList<Integer> createRythm(int bars) {
		ArrayList<Integer> rythms = new ArrayList<Integer>();

		
		for (int i = 0; i < bars; i++) {
			ArrayList<Integer> oneBarRythm = createRythmOneBar();
			for (Iterator<Integer> iterator = oneBarRythm.iterator(); iterator.hasNext();) {
				Integer integer = iterator.next();
				rythms.add(integer);
			}
		}	
		
		return rythms;
	}

	private void setPattern(String pattern) {
		this.pattern = pattern;
	}



	public String getPattern() {
		//System.out.println("motif.getpattern:"+pattern);
		return pattern;
	}
	

}
